<?php

namespace app\common\exception;

use Exception;
use think\exception\Handle;
use think\exception\ValidateException;
use think\image\Exception as ImageException;

class DiyExceptionHandle extends Handle
{
    public function render(Exception $e)
    {
        //自定义异常处理
        if ($e instanceof TokenException || $e instanceof AccountException || $e instanceof DataEmptyException || $e instanceof LogicException) {
            $responseData['meta'] = ['code' => $e->getCode(), 'msg' => $e->getError()];

            return json($responseData);
        }
        // 参数验证异常
        if ($e instanceof ImageException) {
            $responseData['meta'] = ['code' => 0, 'msg' => '图片上传异常，请核实后再试'];

            return json($responseData);
        }
        // 参数验证异常
        if ($e instanceof ValidateException) {
            $responseData['meta'] = ['code' => 0, 'msg' => $e->getError()];

            return json($responseData);
        }
        // 其他错误交给系统处理
        return parent::render($e);
    }
}
